package stack

import (
	"strconv"
	"strings"
)

const LIM = 4

type Stack struct {
	ix  int // The first empty element index
	dat [LIM]int
}

func (s *Stack) Push(d int) bool {
	if s.ix >= LIM {
		return false
	}
	s.dat[s.ix] = d
	s.ix++
	return true
}

func (s *Stack) Pop() (d int) {
	if s.ix <= 0 {
		return
	}
	if s.dat[s.ix-1] != 0 {
		d, s.dat[s.ix-1] = s.dat[s.ix-1], 0
		s.ix--
	}
	return
}

func (s *Stack) String() string {
	str := ""
	for ix, v := range s.dat {
		str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(v) + "] "
	}
	str = strings.TrimSpace(str)
	return str
}
